package com.oromwin.repos.server.request.file;

import java.io.DataInputStream;
import java.io.File;
import java.util.List;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposException;
import com.oromwin.repos.common.helper.DBHelper;
import com.oromwin.repos.common.helper.FSHelper;
import com.oromwin.repos.data.AbstractReposTable;
import com.oromwin.repos.data.FileTable;
import com.oromwin.repos.server.LockObject;
import com.oromwin.repos.server.request.AbstractRequest;
import com.oromwin.repos.server.response.Response;
import com.oromwin.repos.server.response.file.ReadFileResponse;

public class ReadFileRequest extends AbstractRequest {
    private Log log = Logger.getLog(ReadFileRequest.class);
    private String FileId = null;
    
    @Override
    public void receive(DataInputStream dis, LockObject lockObject) throws ReposException {
        synchronized (lockObject) {
            FileId = lockObject.getFileId();
        }
    }

    @Override
    public Response run() throws ReposException {
        ReadFileResponse res = null;
        File file = null;
        try {
            getCurrentReposTables();
            checkSameObjectExists();
            file = _fileTable.getFullFilepath();
            res = new ReadFileResponse(true, "Succeeded to read file", file);
        } catch (ReposException e) {
            if (file == null) {
                file = new File("");
            }
            res = new ReadFileResponse(false, e.getMessage(), file);
        } catch (Exception e) {
            String message = String.format("Unexpected exception, {%s}", e.getMessage());
            throw ReposException.create(log, e, message);
        }
        return res;
    }

    @Override
    public void checkSameObjectExists() throws ReposException {
        File file = _fileTable.getFullFilepath();
        if (!FSHelper.ls(file)) {
            String message = String.format("'%s' FilePath does not exists", file);
            throw ReposException.create(log, message);
        }
    }

    @Override
    public void getCurrentReposTables() throws ReposException {
        AbstractReposTable reposTable = null;
        String whereClause = String.format("FileId = '%s' AND FileStatus = 0", FileId);
        List<AbstractReposTable> reposTables = DBHelper.selectTable(FileTable.class, whereClause);
        if (reposTables != null && reposTables.size() == 1) {
            reposTable = reposTables.get(0);
        } else {
            String message = String.format("'%s' FileId does not exists in database, tableSize:whereClause {%d:%s}", FileId, reposTables.size(), whereClause);
            throw ReposException.create(log, message); 
        }
        if (reposTable instanceof FileTable) {
            _fileTable = (FileTable) reposTable;
        } else {
            String message = String.format("Illegal file table type, class {%s}", reposTable.getClass().toString());
            throw ReposException.create(log, message);
        }
    }

}
